﻿using System.Collections.Generic;
using System.Web.Mvc;

namespace ZQNB.Common.Web.Mvc
{
    public class ThemeableRazorViewEngine : ThemeableVirtualPathProviderViewEngine
    {
        public ThemeableRazorViewEngine()
        {
            AreaViewLocationFormats = new[]
                {
                    "~/Themes/{3}/{2}/{1}/{0}.cshtml",
                    "~/Themes/{3}/{2}/Shared/{0}.cshtml",
                    "~/Areas/{2}/Views/{1}/{0}.cshtml",
                    "~/Areas/{2}/Views/Shared/{0}.cshtml"
                };

            AreaMasterLocationFormats = new[]
                {
                    "~/Themes/{3}/{2}/{1}/{0}.cshtml",
                    "~/Themes/{3}/{2}/Shared/{0}.cshtml",
                    "~/Areas/{2}/Views/{1}/{0}.cshtml",
                    "~/Areas/{2}/Views/Shared/{0}.cshtml"
                };

            AreaPartialViewLocationFormats = new[]
                {
                    "~/Themes/{3}/{2}/{1}/{0}.cshtml",
                    "~/Themes/{3}/{2}/Shared/{0}.cshtml",
                    "~/Areas/{2}/Views/{1}/{0}.cshtml",
                    "~/Areas/{2}/Views/Shared/{0}.cshtml"
                };

            ViewLocationFormats = new[]
                {
                    "~/Themes/{3}/Views/{1}/{0}.cshtml",
                    "~/Themes/{3}/Views/Shared/{0}.cshtml",
                    "~/Themes/{3}/Widget/{1}/{0}.cshtml",
                    "~/Themes/{3}/Widget/Shared/{0}.cshtml",
                    "~/Views/{1}/{0}.cshtml",
                    "~/Views/Shared/{0}.cshtml", 
                    "~/Areas/Widget/Views/{1}/{0}.cshtml", 
                    "~/Areas/Widget/Views/Shared/{0}.cshtml" 
                };


            MasterLocationFormats = new[]
                {
                    "~/Themes/{2}/Views/{1}/{0}.cshtml",
                    "~/Themes/{2}/Views/Shared/{0}.cshtml",
                    "~/Views/{1}/{0}.cshtml",
                    "~/Views/Shared/_Ace/{0}.cshtml", 
                    "~/Views/Shared/_Bs/{0}.cshtml",
                    "~/Views/Shared/{0}.cshtml"
                };

            PartialViewLocationFormats = new[]
                {
                    "~/Themes/{3}/Views/{1}/{0}.cshtml",
                    "~/Themes/{3}/Views/Shared/{0}.cshtml",
                    "~/Views/{1}/{0}.cshtml",
                    "~/Views/Shared/_Ace/{0}.cshtml", 
                    "~/Views/Shared/_Bs/{0}.cshtml", 
                    "~/Views/Shared/{0}.cshtml",
                    "~/Areas/Widget/Views/{1}/{0}.cshtml", 
                    "~/Areas/Widget/Views/Shared/{0}.cshtml" 
                };

            FileExtensions = new[]
                {
                    "cshtml"
                };
        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            string layoutPath = null;
            var runViewStartPages = false;
            IEnumerable<string> fileExtensions = base.FileExtensions;
            return new RazorView(controllerContext, partialPath, layoutPath, runViewStartPages, fileExtensions);
            //return new RazorView(controllerContext, partialPath, layoutPath, runViewStartPages, fileExtensions, base.ViewPageActivator);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            string layoutPath = masterPath;
            var runViewStartPages = true;
            IEnumerable<string> fileExtensions = base.FileExtensions;
            return new RazorView(controllerContext, viewPath, layoutPath, runViewStartPages, fileExtensions);
        }
    }
}
